package com.cloud.www.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.www.model.entity.MFilm;
import com.cloud.www.model.entity.MFilmDetail;
import com.cloud.www.service.MFilmDetailService;
import com.cloud.www.service.MFilmService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//import cn.hutool.db.Page;

@Controller
@AllArgsConstructor
@RequestMapping("")
public class PlayController extends BaseController {

    private final MFilmService filmService;
    private final MFilmDetailService mFilmDetailService;

    /**
     * 详情
     */
    @GetMapping("play")
    public ModelAndView info(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Convert.toInt(request.getParameter("id"), 0);

        MFilmDetail mFilmDetail = mFilmDetailService.getById(id);
        if (null == mFilmDetail) {
            try {
                response.sendRedirect("/");
            } catch (IOException e) {
            }
            return null;
        }
        MFilm mFilm = filmService.getById(mFilmDetail.getFId());
        if (null == mFilm) {
            try {
                response.sendRedirect("/");
            } catch (IOException e) {
            }
            return null;
        }

        ModelAndView view = baseView(mFilm.getName() + "-成长社区", "play/play");
        view.addObject("keyword", "成长社区," + mFilm.getName());


        StringBuilder navTop = new StringBuilder();
        navTop.append(" <a href=\"/o_" + mFilm.getOneTypeId() + ".html\">" + mFilm.getOneTypeName() + "</a>&nbsp;&nbsp;»&nbsp;&nbsp;");
        navTop.append("<a href=\"/t_" + mFilm.getTwoTypeId() + ".html\">" + mFilm.getTwoTypeName() + "</a>");
        view.addObject("topNav", navTop.toString());

        List<MFilm> listRecommend = getRecommendList(mFilm);
        view.addObject("listRecommend", listRecommend);
        view.addObject("data", mFilm);


        List<MFilmDetail> listFilmDetail = mFilmDetailService.selectAll(new MFilmDetail().setFId(mFilm.getId()));
        StringBuilder sbPlayData = new StringBuilder();
        for (MFilmDetail item : listFilmDetail) {
            String[] zykArray = StrUtil.split(item.getZyk(), "$");
            String name = zykArray.length > 0 ? zykArray[0] : "";
            sbPlayData.append("<li ><a title=\"" + name + "\" href=\"/play/" + item.getId() + ".html\" target=\"_self\">" + name + "</a></li>");
        }
        view.addObject("playData", sbPlayData.toString());

        return view;
    }

    //获取推荐列表
    private List<MFilm> getRecommendList(MFilm mFilm) {
        Page pRecommonend = new Page();
        pRecommonend.setSize(12);
        pRecommonend.setCurrent(1);
        List<MFilm> listRecommend = null;
        if (mFilm.getTwoTypeId() != null) {
            //二级分类推荐
            IPage<MFilm> pageRecommend = filmService.selectPage(pRecommonend, new MFilm().setTwoTypeId(mFilm.getTwoTypeId()));
            listRecommend = pageRecommend.getRecords();
        } else if (mFilm.getOneTypeId() != null) {
            //一级分类推荐
            IPage<MFilm> pageRecommend = filmService.selectPage(pRecommonend, new MFilm().setOneTypeId(mFilm.getOneTypeId()));
            listRecommend = pageRecommend.getRecords();
        }
        return listRecommend;
    }


}
